//
//  WBMainViewController.swift
//  CSWeibo
//
//  Created by 白长松 on 16/6/29.
//  Copyright © 2016年 白长松. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
        setupComposeButton()
        
    }
    
    //MARK: - 设置设备支持横屏
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .portrait
    }
    
    //MARK: - composeButton监听方法
    @objc func composeStatus() -> () {
        print("显示撰写界面")
    }
    
    //设置私有控件
    private lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")

}

extension WBMainViewController {
    
    func setupChildControllers() -> () {
        
        //获取沙盒路径
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        
        let jsonPath = (docDir as NSString).appendingPathComponent("Main.json")
        
        var data = NSData(contentsOfFile: jsonPath)
        
        if data == nil {
            //从bundle加载
            let path = Bundle.main().pathForResource("Main.json", ofType: nil)
            data = NSData(contentsOfFile: path!)
        }
        
        //反序列化生成数组
        guard let array = try? JSONSerialization.jsonObject(with: data as! Data, options: []) as? [[String: AnyObject]] else {
            return
        }
        
        
        //根据字典数组生成子控制器数组
        var arrayM = [UIViewController]()
        for dic in array! {
            
            arrayM.append(viewController(dic: dic))
        }
        
        //添加到子控制器数组
        viewControllers = arrayM
        
    }
    
    ///使用字典创建一个字控制器
    func viewController(dic: [String: AnyObject]) -> UIViewController {
        
        //获取字典内容
        guard let clsName = dic["clsName"] as? String,
        title = dic["title"] as? String,
        imageName = dic["imageName"] as? String,
        visitorInfo = dic["visitorInfo"] as? [String: String],
        cls = NSClassFromString(Bundle.main().namespace + "." + clsName) as? WBBaseViewController.Type
            else {
                
            return UIViewController()
        }
        
        //创建一个navigationcontroller
        let vc = cls.init()
        vc.title = title
        
        vc.visitorInfoDictionary = visitorInfo
        
        let nav = WBNavigationController(rootViewController: vc)
        
        
        //设置tabbar的图像
        nav.tabBarItem.image = UIImage(named: imageName)
        nav.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //设置tabbar的文字属性
        let attr = [NSForegroundColorAttributeName: UIColor.orange(), NSFontAttributeName: UIFont.systemFont(ofSize: 12)]
        
        nav.tabBarItem.setTitleTextAttributes(attr, for: .highlighted)
        
        
        return nav
        
    }
    
    //MARK - 设置撰写按钮
    func setupComposeButton() -> () {
        
        tabBar.addSubview(composeButton)
        
        //设置撰写按钮的宽度
        let count = CGFloat(childViewControllers.count)
        let width = tabBar.bounds.width / count - 1
        
        composeButton.frame = tabBar.bounds.insetBy(dx: width * 2, dy: 0)
        
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        
    }
}
